package com.flex.showdoc4j;

import com.flex.core.enums.SdkCommitType;
import com.flex.core.enums.SdkType;
import com.flex.showdoc4j.filter.MvcFilter;
import com.flex.showdoc4j.model.ConfigModel;
import com.flex.showdoc4j.model.GeneratorSdkConfig;
import com.flex.showdoc4j.spring.SimpleSpringDocFactory;
import com.flex.showdoc4j.spring.SpringDocFactory;
import com.flex.showdoc4j.spring.SpringRequestMappingHandler;
import lombok.Builder;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;

import java.io.InputStream;
import java.util.Properties;

/**
 * 产生doc文档
 * @author ganlt<br>
 * @since 2019/06/28 13:18 <br>
 */
@Builder
public class GeneratorShowDoc {

    public static void generator(RequestMappingInfoHandlerMapping handler, ConfigModel configModel, MvcFilter includeMvcFilter) {
        if (configModel == null) {
            configModel = new ConfigModel();
        }
        try {
            Properties properties = getProperties();
            if (StringUtils.isNotEmpty((String) properties.get("author"))) {
                configModel.setAuthor((String) properties.get("author"));
            }
            if (StringUtils.isNotEmpty((String) properties.get("baseUrl"))) {
                configModel.setBaseUrl((String) properties.get("baseUrl"));
            }
            if (StringUtils.isNotEmpty((String) properties.get("createJsonData"))) {
                configModel.setCreateJsonData(Boolean.valueOf((String) properties.get("createJsonData")));
            }
            if (StringUtils.isNotEmpty((String) properties.get("retryCount"))) {
                configModel.setRetryCount(Integer.parseInt((String) properties.get("retryCount")));
            }
            if (StringUtils.isNotEmpty((String) properties.get("saveDir"))) {
                configModel.setSaveDir((String) properties.get("saveDir"));
            }
            if (StringUtils.isNotEmpty((String) properties.get("templateName"))) {
                configModel.setTemplateName((String) properties.get("templateName"));
            }
            if (StringUtils.isNotEmpty((String) properties.get("version"))) {
                configModel.setVersion((String) properties.get("version"));
            }

            if (StringUtils.isNotEmpty((String) properties.get("isGeneratorSdk"))) {
                configModel.setGeneratorSdk(Boolean.valueOf((String) properties.get("isGeneratorSdk")));
            }

            GeneratorSdkConfig generatorSdkConfig = configModel.getGeneratorSdkConfig();
            if (generatorSdkConfig == null) {
                generatorSdkConfig = new GeneratorSdkConfig();
            }
            configModel.setGeneratorSdkConfig(generatorSdkConfig);
            generatorSdkConfig.setConfigModel(configModel);
            if (StringUtils.isNotEmpty((String) properties.get("sdk.templatePath"))) {
                generatorSdkConfig.setSuffixName((String) properties.get("sdk.templatePath"));
            }

            if (StringUtils.isNotEmpty((String) properties.get("sdk.suffixName"))) {
                generatorSdkConfig.setSuffixName((String) properties.get("sdk.suffixName"));
            }

            if (StringUtils.isNotEmpty((String) properties.get("sdk.type"))) {
                generatorSdkConfig.setType(SdkType.valueOf((String) properties.get("sdk.type")));
            }

            if (StringUtils.isNotEmpty((String) properties.get("sdk.sdkCommitType"))) {
                generatorSdkConfig.setSdkCommitType(SdkCommitType.valueOf((String) properties.get("sdk.sdkCommitType")));
            }

            if (StringUtils.isNotEmpty((String) properties.get("sdk.path"))) {
                generatorSdkConfig.setPath((String) properties.get("sdk.path"));
            }

            if (StringUtils.isNotEmpty((String) properties.get("sdk.account"))) {
                generatorSdkConfig.setAccount((String) properties.get("sdk.account"));
            }

            if (StringUtils.isNotEmpty((String) properties.get("sdk.password"))) {
                generatorSdkConfig.setPassword((String) properties.get("sdk.password"));
            }

            if (StringUtils.isNotEmpty((String) properties.get("sdk.authorizePath"))) {
                generatorSdkConfig.setAuthorizePath((String) properties.get("sdk.authorizePath"));
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        SpringRequestMappingHandler springRequestMappingHandler = new SpringRequestMappingHandler(handler);
        SpringDocFactory factory = new SimpleSpringDocFactory();
        configModel.setSaveDir(GeneratorShowDoc.class.getResource("/").getPath()+"/../"+configModel.getSaveDir());
        factory.createDoc(springRequestMappingHandler, configModel,includeMvcFilter);
    }

    public static void generator(RequestMappingInfoHandlerMapping handler, MvcFilter includeMvcFilter) {
        generator(handler,null,includeMvcFilter);
    }

    private static Properties getProperties() {
        Properties properties = new Properties();
        try {
            InputStream in = GeneratorShowDoc.class.getClassLoader().getResourceAsStream("doc.properties");
            properties.load(in);
            return properties;
        } catch (Exception e) {
            return null;
        }
    }
}
